package com.navi.loaddata.loadService.impl.Dcdata.cell1;

import com.navi.loaddata.core.mesEntity.SpcDcResultData;
import com.navi.loaddata.core.mesEntity.SpcDcdata;
import com.navi.loaddata.core.mesEntity.SpcDcmeasuredataresult;
import com.navi.loaddata.core.mesEntity.SpcDcprocessdataresult;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.ProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(transactionManager = "mesPlatformTransactionManager")
public class LoadDcInfoServiceCELL1 {

    @Autowired
    private MesRepositoryImpl mesdao;

    public List<SpcDcdata> loadDcDataList(EtlConfD etlConfD, String dctype) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuffer hql = new StringBuffer();
        hql.append("FROM SpcDcdata WHERE processoperationname<'L4100' and TIMEKEY >='")
                .append(sdf.format(etlConfD.getRunStartTimestamp()))
                .append("' and TIMEKEY < '")
                .append(sdf.format(etlConfD.getRunEndTimestamp()))
                .append("' and subfactoryname = '").append(etlConfD.getShop()).append("' ")
                .append(" and processoperationname not like 'DUM%' AND nvl(SUBSTR(PROCESSOPERATIONNAME, 6,7),'X') <> 'D'");
        if (!StringUtils.isEmpty(etlConfD.getOpe())) {
            hql.append(" and processoperationname = '").append(etlConfD.getOpe()).append("' ");
        }
        hql.append(" and dctype IN(").append(dctype).append(")");

        return mesdao.list(SpcDcdata.class, hql.toString());
    }

    public List<SpcDcResultData> loadMDataList(EtlConfD etlConfD, long dcdataid) {
        List<SpcDcmeasuredataresult> spcDcmeasuredataresultList =new ArrayList<>();
        List<SpcDcResultData> spcDcResultDataList=new ArrayList<>();
        StringBuffer hql = new StringBuffer();
        hql.append("FROM SpcDcmeasuredataresult WHERE dcdataid =").append(dcdataid)
        .append(" order by itemname, sitename");
        spcDcmeasuredataresultList =mesdao.list(SpcDcmeasuredataresult.class, hql.toString());
        spcDcmeasuredataresultList.forEach(e->{
            SpcDcResultData spcDcResultData=new SpcDcResultData();
            spcDcResultData.setDcdataid(e.getDcdataid());
            spcDcResultData.setSeq(e.getSeq());
            spcDcResultData.setSamplematerialname(ProcessUtil.processString(e.getSamplematerialname()));
            spcDcResultData.setItemname(ProcessUtil.processString(e.getItemname()));
            spcDcResultData.setSitename(ProcessUtil.processString(e.getSitename()));
            spcDcResultData.setSitetype(ProcessUtil.processString(e.getSitetype()));
            spcDcResultData.setAliasitemname(ProcessUtil.processString(e.getAliasitemname()));
            spcDcResultData.setDatatype(ProcessUtil.processString(e.getDatatype()));
            spcDcResultData.setResult(ProcessUtil.processString(e.getResult()));
            spcDcResultData.setEventname(ProcessUtil.processString(e.getEventname()));
            spcDcResultData.setTimekey(ProcessUtil.processString(e.getTimekey()));
            spcDcResultData.setEventtime(e.getEventtime());
            spcDcResultData.setEventuser(ProcessUtil.processString(e.getEventuser()));
            spcDcResultData.setEventcomment(ProcessUtil.processString(e.getEventcomment()));
            spcDcResultData.setUpdatetime(e.getUpdatetime());
            spcDcResultDataList.add(spcDcResultData);
        });
        return spcDcResultDataList;
    }

    public List<SpcDcResultData> loadPDataList(EtlConfD etlConfD, long dcdataid) {
        List<SpcDcprocessdataresult> spcDcprocessdataresult =new ArrayList<>();
        List<SpcDcResultData> spcDcResultDataList=new ArrayList<>();
        StringBuffer hql = new StringBuffer();
        hql.append("FROM SpcDcprocessdataresult WHERE dcdataid =").append(dcdataid)
                .append(" order by itemname, sitename");
        spcDcprocessdataresult =mesdao.list(SpcDcprocessdataresult.class, hql.toString());
        spcDcprocessdataresult.forEach(e->{
            SpcDcResultData spcDcResultData=new SpcDcResultData();
            spcDcResultData.setDcdataid(e.getDcdataid());
            spcDcResultData.setSeq(e.getSeq());
            spcDcResultData.setSamplematerialname(ProcessUtil.processString(e.getSamplematerialname()));
            spcDcResultData.setItemname(ProcessUtil.processString(e.getItemname()));
            spcDcResultData.setSitename(ProcessUtil.processString(e.getSitename()));
            spcDcResultData.setSitetype(ProcessUtil.processString(e.getSitetype()));
            spcDcResultData.setAliasitemname(ProcessUtil.processString(e.getAliasitemname()));
            spcDcResultData.setDatatype(ProcessUtil.processString(e.getDatatype()));
            spcDcResultData.setResult(ProcessUtil.processString(e.getResult()));
            spcDcResultData.setEventname(ProcessUtil.processString(e.getEventname()));
            spcDcResultData.setTimekey(ProcessUtil.processString(e.getTimekey()));
            spcDcResultData.setEventtime(e.getEventtime());
            spcDcResultData.setEventuser(ProcessUtil.processString(e.getEventuser()));
            spcDcResultData.setEventcomment(ProcessUtil.processString(e.getEventcomment()));
            spcDcResultData.setUpdatetime(e.getUpdatetime());
            spcDcResultDataList.add(spcDcResultData);
        });
        return spcDcResultDataList;
    }


    private String getOpeHql(String confOpe) {
        if (StringUtils.isEmpty(confOpe)) {
            return "";
        }

        StringBuffer hql = new StringBuffer();
        if ("*".contains(confOpe)) {
            //todo 允许模糊匹配
        } else {
            hql.append(" and opeId='").append(confOpe).append("' ");
        }

        return hql.toString();
    }


}
